<template>
    <div class="header">
        <div class="logo">
            <span>ROCKY-BEAM</span>
        </div>
        <div class="headMenu">
          <el-menu :default-active="activeIndex" mode="horizontal" @select="handleSelect">
              <el-menu-item  v-for="item in menulist" :index="item.route" :key="item.id">
                  {{item.name}}
              </el-menu-item>
          </el-menu>
        </div>
        <div class="user-info">
          <div class="user-touxiang pic" @click="drawer = true">
            <img class="user-logo" src="../../assets/img/guanliyuan.png">
          </div>
          <el-drawer
            :title="$t('login.login')"
            :visible.sync="drawer"
            size="10%">
            <div class="ml20 headDrawer">
                <p class="mb20">{{$t('adminHome.no46')}}：<span class="name">{{username}}</span></p>
                <el-button @click="innerDrawer = true" type="primary" size="small">{{$t('header.changePass')}}</el-button>
                <el-drawer
                  :title="$t('header.changePass')"
                  :append-to-body="true"
                  :visible.sync="innerDrawer">
                  <div class="changepass">
                      <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="120px" class="demo-ruleForm">
                          <el-form-item :label="$t('header.oldPassWord')" prop="oldPassword">
                              <el-input type="password" v-model="ruleForm2.oldPassword" auto-complete="off"></el-input>
                          </el-form-item>
                          <el-form-item :label="$t('header.newPassWord')" prop="password">
                              <el-input type="password" v-model="ruleForm2.password" auto-complete="off"></el-input>
                          </el-form-item>
                          <el-form-item :label="$t('header.newPassWordTwo')" prop="checkPass">
                              <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
                          </el-form-item>
                          <el-form-item>
                              <el-button type="primary" @click="submitForm('ruleForm2')">{{$t('header.submit')}}</el-button>
                              <!-- <el-button @click="close">{{$t('header.cancel')}}</el-button> -->
                          </el-form-item>
                      </el-form>
                  </div>
                </el-drawer>
                <div class="mt20"><el-button @click="loginout" type="primary" size="small">{{$t('header.exit')}}</el-button></div>
              </div>
            </el-drawer>
          </div>
    </div>
</template>
<script>
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('login.required')))
      } else if (value.length <= 5) {
        callback(new Error(this.$t('login.pass.no4')))
      } else {
        if (this.ruleForm2.checkPass !== '') {
          this.$refs.ruleForm2.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('login.pass.no1')))
      } else if (value !== this.ruleForm2.password) {
        callback(new Error(this.$t('login.pass.no2')))
      } else if (value === this.ruleForm2.oldPassword) {
        callback(new Error(this.$t('login.pass.no3')))
      } else {
        callback()
      }
    }
    var validatePass3 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('login.required')))
      } else {
        callback()
      }
    }
    return {
      activeIndex:sessionStorage.topMenu,
      menulist:[],
      sessionid: '',
      header: '',
      headerTitle: '',
      flag: false,
      drawer: false,
      innerDrawer:false,
      ruleForm2: {
        oldPassword: '',
        password: '',
        checkPass: ''
      },
      rules2: {
        oldPassword: [
            {validator: validatePass3, trigger: 'blur'}
        ],
        password: [
            { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
            { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    onRoutes () {
      return this.$route.path.replace('/', '')
    },
    username () {
      let username = sessionStorage.getItem('nickName')
      return username || ''
    }
  },
  methods: {
    handleSelect(key, keyPath) {
        sessionStorage.setItem("topMenu",key)
        // this.bus.$emit('topmenu', key)
        this.$store.commit('setTopMenu',sessionStorage.topMenu);
    },
    loginout () {
      sessionStorage.clear()
      this.$router.push('/login')
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // eslint-disable-next-line
          let jse = new JSEncrypt()
          jse.setPublicKey(this.$store.state.common.publicKey)
          //   设置需要加密的字符串
          let option = {
            param:{
              newPassword: jse.encrypt(this.ruleForm2.password),
              oldPassword: jse.encrypt(this.ruleForm2.oldPassword)
            }
          }
          this.$axios.post(this.$action.modifyPwd, option).then(res => {
            if (res.code ==200) {
              this.$globalMsg.success(this.$t('idManage.no5'))
              setTimeout(() => {
                this.loginout()
              }, 1000)
            } else {
              this.$globalMsg.error(this.$t('idManage.no6'))
            }
          })
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  },
  mounted () {
    this.sessionid = sessionStorage.getItem('sessionid')
    this.header = global.config.headerImg
    this.headerTitle = global.config.headerTotal
    this.menulist=JSON.parse(sessionStorage.getItem('menulist'))
    this.$store.commit('setTopMenu',sessionStorage.topMenu);
  }
}
</script>
